//
//  ViewController.m
//  RuntimeDemo
//
//  Created by zxs on 2017/6/12.
//  Copyright © 2017年 zxs. All rights reserved.
//

#import "ViewController.h"
#import "TestClass.h"
#import <objc/message.h>
#import "UIButton+Click.h"
#import "TestModel.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextView *logTextView;
@property (weak, nonatomic) IBOutlet UIButton *associatedButton;
@property (weak, nonatomic) IBOutlet UIButton *clearButton;
@property (weak, nonatomic) IBOutlet UIButton *messageSendButton;
@property (weak, nonatomic) IBOutlet UIButton *archiveButton;

@end

@implementation ViewController

# pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    __weak __typeof(self)weakSelf = self;
    __strong __typeof(weakSelf)strongSelf = weakSelf;
    
    self.associatedButton.click = ^{
        [strongSelf appendLog:@"对象关联按钮被点击"];
    };
    
    self.clearButton.click = ^{
        strongSelf.logTextView.text = @"";
    };
    
    self.messageSendButton.click = ^{
        [strongSelf messageSend];
    };
    
    self.archiveButton.click = ^{
        [strongSelf keyedArchiver];
    };
}

# pragma mark - function
- (void)messageSend {
    TestClass *objct = [[TestClass alloc] init];
    
    NSInteger age = ((NSInteger (*) (id, SEL)) objc_msgSend) (objct, sel_registerName("showAge"));
    [self appendLog:[NSString stringWithFormat:@"Age is %zd", age]];
    
    NSString *name = ((NSString* (*) (id, SEL, NSString*)) objc_msgSend) (objct, sel_registerName("showName:"), @"Dave Ping");
    [self appendLog:name];
    
    NSString *size = ((NSString* (*) (id, SEL, CGFloat, CGFloat)) objc_msgSend) (objct, sel_registerName("showWidth:andHeight:"), 110.5f, 200.0f);
    [self appendLog:size];
    
    CGFloat height = ((CGFloat (*) (id, SEL)) objc_msgSend_fpret) (objct, sel_registerName("getHeight"));
    // 这里用objc_msgSend也是可以的；objc_msgSend_fpret更精确一点而已
//    CGFloat height = ((CGFloat (*) (id, SEL)) objc_msgSend) (objct, sel_registerName("getHeight"));
    [self appendLog:[NSString stringWithFormat:@"Height is %0.2f", height]];
    
    NSString *info = ((NSString* (*) (id, SEL)) objc_msgSend) (objct, sel_registerName("getInfo"));
    [self appendLog:info];
}

- (void)keyedArchiver {
    TestModel *model = [[TestModel alloc] init];
    model.name = @"Dave Ping";
    model.age = @24;
    model.phoneNumber = @18718871111;
    model.son = [[TestModel alloc] init];
    model.son.name = @"Jack";
    [self appendLog:[NSString stringWithFormat:@"model.name is %@", model.name]];
    [self appendLog:[NSString stringWithFormat:@"model.name is %@", model.son.name]];
    
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    path = [path stringByAppendingPathComponent:@"test"];
    [NSKeyedArchiver archiveRootObject:model toFile:path];
    
    TestModel *m = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    [self appendLog:[NSString stringWithFormat:@"m.name is %@", m.name]];
    [self appendLog:[NSString stringWithFormat:@"m.name is %@", m.son.name]];
}


# pragma mark - private
- (void)appendLog: (NSString *)log {
    [self appendLog:log withLine:YES];
}

- (void)appendLog: (NSString *)log withLine:(BOOL)isAddLine {
    if ((nil == log) || (0 == log.length)) {
        return;
    }
    NSMutableString *text = [NSMutableString stringWithString:self.logTextView.text];
    [text appendString:log];
    if (isAddLine) {
        [text appendString:@"\n"];
    }
    self.logTextView.text = [text copy];
}


@end
